package com.zkt.subject.domain.handler.subject;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zkt.subject.common.enums.SubjectInfoTypeEnum;
import com.zkt.subject.domain.convert.SubjectBriefConverter;
import com.zkt.subject.domain.entity.SubjectUserScore;
import com.zkt.subject.infra.entity.SubjectInfoBO;
import com.zkt.subject.domain.entity.SubjectOptionBO;
import com.zkt.subject.infra.basic.entity.SubjectBrief;
import com.zkt.subject.infra.basic.service.ISubjectBriefService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/10/6
 * @description 简答题的策略类
 **/

@Component
public class BriefTypeHandler implements SubjectTypeHandler {
	
	@Resource
	private ISubjectBriefService subjectBriefService;
	
	/**
	 * 枚举身份的识别
	 *
	 * @return 题目类型的枚举
	 */
	@Override
	public SubjectInfoTypeEnum getHandlerType() {
		return SubjectInfoTypeEnum.BRIEF;
	}
	
	/**
	 * 实际的题目的插入
	 *
	 * @param subjectInfoBO 题目数据
	 */
	@Override
	public void add(SubjectInfoBO subjectInfoBO) {
		// 简答题目的插入
		SubjectBrief subjectBrief = SubjectBriefConverter.INSTANCE.convertBoToBrief(subjectInfoBO);
		subjectBrief.setId(null);
		subjectBrief.setSubjectId(subjectInfoBO.getId());
		subjectBriefService.save(subjectBrief);
	}
	
	/**
	 * 实际的题目的删除
	 *
	 * @param subjectId 题目id
	 */
	@Override
	public void delete(Long subjectId) {
		if (subjectId != null) {
			subjectBriefService.lambdaUpdate()
					.eq(SubjectBrief::getSubjectId, subjectId)
					.remove();
		}
	}
	
	/**
	 * 实际的题目的更新
	 *
	 * @param subjectInfoBO 题目数据
	 */
	@Override
	public void update(SubjectInfoBO subjectInfoBO) {
		// 简答题的更新
		if (ObjUtil.isNull(subjectInfoBO.getId()) || StrUtil.isBlank(subjectInfoBO.getSubjectAnswer())) {
			this.delete(subjectInfoBO.getId());
			this.add(subjectInfoBO);
		}
	}
	
	/**
	 * 根据题目id查询对应题目信息
	 *
	 * @param subjectId 题目id
	 * @return 题目信息
	 */
	@Override
	public SubjectOptionBO queryBySubjectId(Long subjectId) {
		SubjectBrief subjectBrief = subjectBriefService.lambdaQuery().eq(SubjectBrief::getSubjectId, subjectId).one();
		if (ObjectUtil.isNull(subjectBrief)) {
			return null;
		}
		SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
		subjectOptionBO.setSubjectAnswer(subjectBrief.getSubjectAnswer());
		return subjectOptionBO;
	}
	
	/**
	 * 获取用户得分
	 *
	 * @param subjectInfoBO 用户答案信息
	 * @return 用户得分
	 */
	@Override
	public SubjectUserScore getUserScore(SubjectInfoBO subjectInfoBO) {
		SubjectUserScore subjectUserScore = new SubjectUserScore();
		if (StrUtil.isBlank(subjectInfoBO.getSubjectAnswer())) {
			return subjectUserScore;
		}
		
		SubjectBrief subjectBrief = subjectBriefService.lambdaQuery().eq(SubjectBrief::getSubjectId, subjectInfoBO.getId()).one();
		if (ObjUtil.isNull(subjectBrief)) {
			return subjectUserScore;
		}
		
		// TODO：使用AI进行简答题得分判断
		
		subjectUserScore.setUserAnswer(subjectInfoBO.getSubjectAnswer());
		subjectUserScore.setUserScore(subjectInfoBO.getSubjectScore() / 2);
		
		return subjectUserScore;
	}
}
